package study.cv.image.auth;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class WatchdogFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain chain) throws ServletException, IOException {
        Stopwatch stopwatch = Stopwatch.createStarted();

        try {
            //next filter
            doFilter(request, response, chain);
        } catch (Exception ex) {
            throw ex;
        } finally {
            log.info("cost time [{}] ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
    }
}
